package com.main.ok.lucene;

import java.io.IOException;
import java.nio.file.Paths;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.FSDirectory;

/**
 * 
 * @Type MySearcher
 * @Desc 对结果集搜索
 * @author Administrator
 * @date 2015年3月12日
 * @Version V1.0
 */
public class MySearcher {
    public static final String index = "g:/lucene";
    public static final String field = "contents";

    public static void main(String[] args) throws IOException, ParseException {
        FSDirectory directory = FSDirectory.open(Paths.get(index));
        DirectoryReader reader = DirectoryReader.open(directory);
        IndexSearcher search = new IndexSearcher(reader);

        Analyzer ana = new StandardAnalyzer();

        QueryParser parse = new QueryParser(field, ana);
        Query s = parse.parse("xml");

        TopDocs docs = search.search(s, null, 100);
        System.out.println(docs.totalHits);
        ScoreDoc[] sdoc = docs.scoreDocs;
        int docId = sdoc[1].doc;
        Document document = search.doc(docId);
        System.out.println(document.get("modified"));
    }
}
